<?php
	
	
	
	
	
	/**
	 * 
	 * Ldap
	 * 
	 * Class to manage the LDAP configuration and connection
	 */
	class Ldap {
		
		/**
		 * @var string $ldap_server_name
		 */
		private $ldap_server_name			= '127.0.0.1';
		
		/**
		 * @var string $ldap_server_domain
		 */
		private $ldap_server_domain			= '127.0.0.1';
		
		/**
		 * @var string $ldap_domain_name
		 */
		private $ldap_domain_name			= 'dc=example,dc=company,dc=com';
		
		/**
		 * @var string $ldap_username
		 */
		private $ldap_username				= '';
		
		/**
		 * @var string $ldap_password
		 */
		private $ldap_password				= '';
		
		/**
		 * @var object $ci
		 */
		private $ci							= NULL;
		
		
		
		
		
		/**
		 * 
		 * __construct
		 * 
		 * @param Array $config
		 * 
		 * the $config var have the next configuration
		 * $config = array(
		 * 		'ldap_server_name',
		 * 		'ldap_server_domain',
		 * 		'ldap_domain_name',
		 * 		'ldap_username',
		 * 		'ldap_password'
		 * );
		 */
		public function __construct ( $config = array() ) {
			
			// getting the Codeigniter Core Data to use in this library
			$this->ci =& get_instance();
			
			// initializing the parameters
			if ( count($config) > 0 ) $this->initialize($config);
		}
		// end: __construct
		
		
		
		
		
		/**
		 * 
		 * initialize
		 * 
		 * method to initialize the params
		 * 
		 * @param Array $configs
		 */
		public function initialize ( $config = array() ) {
			
			// setting up the params
			if ( !empty($config) ) {
				$this->ldap_server_name			= $config['ldap_server_name'];
				$this->ldap_server_domain		= $config['ldap_server_domain'];
				$this->ldap_domain_name			= $config['ldap_domain_name'];
				$this->ldap_username			= $config['ldap_username'];
				$this->ldap_password			= $config['ldap_password'];
			}
		}
		// end: initialize
		
		
		
		
		
		/**
		 * 
		 * set_connection
		 * method to set the connection to the server directory
		 * 
		 * @return integer
		 * 
		 * @see http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
		 */
		public function set_connection () {
			
			// beg: setting the connection
			if ( @$ldap_connection = @ldap_connect($this->ldap_server_name) ) {
				
				// setting protocol version
				ldap_set_option($conectado_LDAP, LDAP_OPT_PROTOCOL_VERSION, 3);
				
				// setting the referals
				ldap_set_option($conectado_LDAP, LDAP_OPT_REFERRALS, 0);
				
				// beg: setting connection binding
				if ( @$ldap_authenticated = @ldap_bind($ldap_connection, $this->ldap_username . '@' . $this->ldap_server_domain, $this->ldap_password) )
					return 200;				// success 200		: OK
				else
					return 401;				// error 401		: Unauthorized
				// end: setting connection binding
			}
			// end: setting the connection
			
			else
				return 503;					// error 503		: Service unavailable
			
		}
		// end: set_connection
		
		
		
		
		
		/**
		 * 
		 * set_connection_true
		 * method only to test the system connection
		 * 
		 * this method is fake
		 */
		public function set_connection_true () {
			return 200;						// success 200		: OK
		}
		// end: set_connection_true
	}
	// end: Ldap